package com.muyi.biling;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.text.TextUtils;

public class SharedPreferencesHelper {

    SharedPreferences mSharedPref;

    public SharedPreferencesHelper(Context context) {
        this(context, "");
    }

    public SharedPreferencesHelper(Context context, String sharedPrefName) {
        if (TextUtils.isEmpty(sharedPrefName)) {
            mSharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        } else {
            mSharedPref = context.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE);
        }
    }

    /**
     The best way of storing double values in SharedPreferences without losing precision is:
     Transform to bit representation to store it as long:
     prefsEditor.putLong("Latitude", Double.doubleToLongBits(location.getLatitude()));
     To retrieve, transfrom from bit representation to double:
     double latitude = Double.longBitsToDouble(prefs.getLong("Latitude", 0);
     */

    /**
     * if type of defvalue is not equal clazz
     *
     * @param defValue
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getData(String key, @NonNull Object defValue, @NonNull Class<T> clazz) {
        T t = null;
        if (!defValue.getClass().getSimpleName().equals(clazz.getSimpleName())) {
            // TODO:
            throw new UnsupportedOperationException("defValue type does not equals whit clazz "); //转换将失败！
        }

        switch (clazz.getSimpleName()) {
            case "String":
                t = (T) mSharedPref.getString(key, (String) defValue);
                break;
            case "Integer":
                t = (T) (Integer) mSharedPref.getInt(key, (Integer) defValue);
                break;
            case "Float":
                t = (T) (Float) mSharedPref.getFloat(key, (Float) defValue);
                break;
            case "Long":
                t = (T) (Long) mSharedPref.getLong(key, (Long) defValue);
                break;
//			case "Double":
//				t = (T) (Double) sharedPreferences.get
//				break;
            case "Boolean":
                t = (T) (Boolean) mSharedPref.getBoolean(key, (Boolean) defValue);
                break;
        }
        return t;
    }

    public String getString(String key, @NonNull Object defValue) {
        if (!defValue.getClass().getSimpleName().equals("String")) {
            // TODO:
            throw new UnsupportedOperationException("defValue type does not equals whit clazz "); //转换将失败！
        }
        return mSharedPref.getString(key, (String) defValue);
    }

    public Integer getInt(String key, @NonNull Object defValue) {
        if (!defValue.getClass().getSimpleName().equals("Integer")) {
            // TODO:
            throw new UnsupportedOperationException("defValue type does not equals whit clazz "); //转换将失败！
        }
        return mSharedPref.getInt(key, (Integer) defValue);
    }

    public Float getFloat(String key, @NonNull Object defValue) {
        if (!defValue.getClass().getSimpleName().equals("Float")) {
            // TODO:
            throw new UnsupportedOperationException("defValue type does not equals whit clazz "); //转换将失败！
        }
        return mSharedPref.getFloat(key, (Float) defValue);
    }

    public Long getLong(String key, @NonNull Object defValue) {
        if (!defValue.getClass().getSimpleName().equals("Long")) {
            // TODO:
            throw new UnsupportedOperationException("defValue type does not equals whit clazz "); //转换将失败！
        }
        return mSharedPref.getLong(key, (Long) defValue);
    }

    public Boolean getBoolean(String key, @NonNull Object defValue) {
        if (!defValue.getClass().getSimpleName().equals("Boolean")) {
            // TODO:
            throw new UnsupportedOperationException("defValue type does not equals whit clazz "); //转换将失败！
        }
        return mSharedPref.getBoolean(key, (Boolean) defValue);
    }

    /**
     * 存储数据
     *
     * @param key
     * @param value
     */
    public void setData(String key, @NonNull Object value) {
        if (value instanceof String) {
            mSharedPref.edit().putString(key, (String) value).commit();
        } else if (value instanceof Integer) {
            mSharedPref.edit().putInt(key, (Integer) value).commit();
        } else if (value instanceof Float) {
            mSharedPref.edit().putFloat(key, (Float) value).commit();
        } else if (value instanceof Long) {
            mSharedPref.edit().putLong(key, (Long) value).commit();
        } else if (value instanceof Boolean) {
            mSharedPref.edit().putBoolean(key, (Boolean) value).commit();
        }
    }
}
